<?php


namespace app\common\model;


use app\api\library\Domain;
use think\Model;

class Adproject extends Model
{
    // 表名
    protected $name = 'ad_project';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'effectime_text',
        'expiretime_text'
    ];

    protected static function init()
    {
        self::beforeInsert(static function ($row) {
            if (!$row->weigh) {
                $count = Adproject::max('weigh');
                $row->weigh = $count + 1;
            }
        });
    }

    public function getImageAttr($value, $data)
    {
        return Domain::http($value);
    }

    protected function getEffectimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['effectime'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function getExpiretimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['expiretime'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setEffectimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setExpiretimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function demand()
    {
        return $this->belongsTo(Demand::class);
    }
    public function goods()
    {
        return $this->belongsTo(Goods::class);
    }
}